package com.yuxin.wx.controller;

import com.alibaba.druid.util.StringUtils;
import com.yuxin.wx.dao.EsBlogRepository;
import com.yuxin.wx.domain.EsBlog;
import com.yuxin.wx.result.CodeMsg;
import com.yuxin.wx.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;

/**
 * @author zhangchuanqiang
 * @Description: Es 测试类
 */

@RestController
@RequestMapping("/es")
public class EsController {
	@Autowired
	private EsBlogRepository esBlogRepository;

	@Autowired
	private ElasticsearchTemplate elasticsearchTemplate;

	/**
	 * @Description: 初始化数据
	 * @Author: zcq
	 * @Date: 2019-05-14 11:27
	 */
	@PostMapping("/initData")
	public String initData() {
		// 清除所有数据
		esBlogRepository.deleteAll();
		esBlogRepository.save(new EsBlog("登黄雀楼", "黄雀楼", "百日依山尽"));
		esBlogRepository.save(new EsBlog("相思", "相思王维", "红豆生南国"));
		esBlogRepository.save(new EsBlog("静夜思", "静夜思李白", "窗前明月光"));
		return "init success";
	}

	/**
	 * @Description: 删除数据
	 * @Author: zcq
	 * @Date: 2019-05-14 11:27
	 */
	@PostMapping("/delete")
	public Result deleteById() {
		esBlogRepository.deleteAll();
		return Result.success("删除成功");
	}

	@GetMapping("testEs")
	public Result testEs() {
		Pageable pageable = PageRequest.of(0, 20);
		Page<EsBlog> page = esBlogRepository.findDistinctEsBlogByTitleContainingOrContentContaining("白", "山", pageable);
		return Result.success(page.getContent());
	}

	@GetMapping("/get/{id}")
	public Result getById(@PathVariable String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error(CodeMsg.SERVER_ERROR);
		}
		Optional<EsBlog> blogModelOptional = esBlogRepository.findById(id);
		if (blogModelOptional.isPresent()) {
			EsBlog blogModel = blogModelOptional.get();
			return Result.success(blogModel);
		}
		return Result.error(CodeMsg.SERVER_ERROR);
	}

	@GetMapping("/get")
	public Result getAll() {
		Iterable<EsBlog> iterable = esBlogRepository.findAll();
		List<EsBlog> list = new ArrayList<>();
		iterable.forEach(list::add);
		return Result.success(list);
	}

	@PostMapping("/update")
	public Result updateById(@RequestBody EsBlog blogModel) {
		String id = blogModel.getId();
		if (StringUtils.isEmpty(id)) {
			return Result.error(CodeMsg.SERVER_ERROR);
		}
		esBlogRepository.save(blogModel);
		return Result.success();
	}

	@DeleteMapping("/delete/{id}")
	public Result deleteById(@PathVariable String id) {
		if (StringUtils.isEmpty(id)) {
			return Result.error();
		}
		esBlogRepository.deleteById(id);
		return Result.success();
	}

	@GetMapping("/rep/search/title")
	public Result repSearchTitle(String keyword) {
		if (StringUtils.isEmpty(keyword)) {
			return Result.error();
		}
		return Result.success(esBlogRepository.findDistinctEsBlogByTitleLike(keyword));
	}

	@GetMapping("/search/title")
	public Result searchTitle(String keyword) {
		if (StringUtils.isEmpty(keyword)) {
			return Result.error();
		}
		SearchQuery searchQuery = new NativeSearchQueryBuilder()
				.withQuery(queryStringQuery(keyword))
				.build();
		List<EsBlog> list = elasticsearchTemplate.queryForList(searchQuery, EsBlog.class);
		return Result.success(list);
	}
}

